<template>
  <el-dialog :title="!dataForm.id ? '新增' : '修改'" :close-on-click-modal="false" :visible.sync="visible" width="30%">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()"
      label-width="120px">
      <el-form-item label="帮助对象" prop="recipientId">
        <el-select v-model="dataForm.recipientId" filterable clearable placeholder="帮助对象" @change="setFormVale">
          <el-option v-for="item in recipients" :key="item.id" :label="item.name + ' ' + item.categoryName + ' ' + item.age"
            :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="年龄" prop="age">
        <el-input v-model="dataForm.age" placeholder="年龄" readonly></el-input>
      </el-form-item>
      <el-form-item label="性别" prop="gender">
        <el-input  placeholder="性别" v-model="dataForm.gender" readonly></el-input>
      </el-form-item>
      <el-form-item label="对接人" prop="fellowName">
        <el-input v-model="dataForm.fellowName" placeholder="对接人" readonly></el-input>
      </el-form-item>
      <el-form-item label="对接人电话" prop="fellowTel">
        <el-input v-model="dataForm.fellowTel" placeholder="对接人电话" readonly></el-input>
      </el-form-item>
      <el-form-item label="需求描述" prop="detail">
        <el-input v-model="dataForm.detail" placeholder="需求描述" ></el-input>
      </el-form-item>
      <el-form-item label="建立时间" prop="establishTime">
        <el-date-picker
            v-model="dataForm.establishTime"
            value-format="yyyy-MM-dd HH:mm:ss"
            style="width: 300px"
            type="datetime"
            placeholder="建立时间">
        </el-date-picker>
        
      </el-form-item>
      <el-form-item label="紧急程度" prop="emergencyGrade">
        <el-select v-model="dataForm.emergencyGrade" placeholder="紧急程度" clearable>
          <el-option v-for="item in emergencyGradeType" :key="item.key" :label="item.value" :value="item.key">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="需求是否解决" prop="isSlove">
        <el-select v-model="dataForm.isSlove">
          <el-option v-for="item in requiredType" :key="item.key" :label="item.value" :value="item.key">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="需求解决时间" prop="solveTime">
        <el-date-picker v-model="dataForm.solveTime" value-format="yyyy-MM-dd HH:mm:ss" type="datetime"
          placeholder="需求解决时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="帮助描述" prop="helpRemark">
        <el-input v-model="dataForm.helpRemark" placeholder="帮助描述"></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import service from "@/utils/httpRequest";

export default {
  data() {
    return {
      visible: false,
      emergencyGradeType: [
        {
          key: 1,
          value: '第一等级'
        },
        {
          key: 2,
          value: '第二等级'
        },
        {
          key: 3,
          value: '第三等级'
        },
      ],
      recipients: [
        {
          id: 0,
          name: '',
          categoryName: '',
          gender: '',
          age: ''
        }
      ],
      requiredType: [
        {
          key: 1,
          value: '是',
        },
        {
          key: 0,
          value: '否'
        }
      ],
      dataForm: {
        id: 0,
        recipientId: '',
        recipientName: '',
        age: '',
        gender: '',
        fellowName: '',
        fellowTel: '',
        detail: '',
        establishTime: '',
        emergencyGrade: '',
        isSlove: '',
        solveTime: '',
        helpRemark: ''
      },
      dataRule: {
        establishTime: [
          { required: true, message: '需求建立时间不能为空', trigger: 'blur' }
        ],
        detail: [
          { required: true, message: '需求描述不能为空', trigger: 'blur' }
        ],
        isSlove: [
          { required: true, message: '需求是否解决不能为空', trigger: 'blur' }
        ],
        emergencyGrade: [
          { required: true, message: '需求紧急程度不能为空', trigger: 'blur' }
        ],
        recipientId: [
          { required: true, message: '受助者号不能为空', trigger: 'blur' }
        ],

      }
    }
  },
  methods: {
    setFormVale(value) {

      service.get(`/lovehelp/recipient/info/${value}`, {}).then((response) => {
        if (response && response.code === 200) {

          this.dataForm.gender = response.recipient.gender=='0'?'男':'女'
          this.dataForm.age = response.recipient.age
          this.dataForm.fellowId = response.recipient.fellowId
          this.dataForm.fellowName = response.recipient.fellowName
          this.dataForm.fellowTel = response.recipient.fellowTel
        }
      }).catch((error) => {
        // 处理错误  
        console.error(error);
      });
    },
    getRecipients() {
      service.get('/lovehelp/recipient/allrecipient').then((response) => {
        if (response && response.code === 200) {
          this.recipients = response.recipients
        } else {
          this.recipients = []
        }
      }).catch((error) => {
        console.error(error)
      })
    },
    init(id) {
      this.getRecipients()
      this.dataForm.id = id || 0
      this.visible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        if (this.dataForm.id) {
          service.get(`/lovehelp/need/info/${this.dataForm.id}`, {}).then((res) => {
            if (res.code === 200) {
              this.dataForm = res.need;
              this.dataForm.gender = res.need.gender=='0'?'男':'女'
            }
          })
        }
      })
    },
    // 表单提交
    dataFormSubmit() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          service({
            method: 'POST',
            url: `/lovehelp/need/${!this.dataForm.id ? 'save' : 'update'}`,
            data: {
              'id': this.dataForm.id || undefined,
              'establishTime': this.dataForm.establishTime,
              'solveTime': this.dataForm.solveTime,
              'detail': this.dataForm.detail,
              'isSlove': this.dataForm.isSlove,
              'emergencyGrade': this.dataForm.emergencyGrade,
              'recipientId': this.dataForm.recipientId,
              'helpRemark': this.dataForm.helpRemark
            }
          }).then(res => {
            if (res.code === 200) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.visible = false
                  this.$emit('refreshDataList')
                }
              })
            } else {
              this.$message.error(res.msg)
            }
          })
        }
      })
    }
  }
}
</script>
